var arrMgmtGroup = LB.CONFIG.global.qq_group.management
var arrChatGroup = LB.CONFIG.global.qq_group.chat

function MgmtGroup(e){
  let qq = e.user_id
  let msg = e.raw_message
  if(e.group_id != arrMgmtGroup){   //发信人所在群和设置的不一致，则直接返回，不执行
    return;
  }

  if(LB.HELPER.REGEX.getRegexPermission(e.raw_message) == 1 && LB.HELPER.APD.getPermission(e.user_id) != 1){
    e.reply("[CQ:at,qq="+qq+"] "+LB.HELPER.MSG.getGroupEvent("player_permission_denied"))
    return
  }
  if (LB.HELPER.REGEX.getRegexNum(e.raw_message) == false){
    return
  }

  let actions = LB.HELPER.REGEX.getRegexActions(e.raw_message)
  for(a in actions){
    var type = actions[a].type
    var content = actions[a].content
    switch(type){
      default:LB.log.warn(`未知的动作模式：${type}，请检查拼写是否有误。`);break;

      case "group_msg":   //向群内发送消息
        LB.bot.toMgmtGroup(content)
      break;

      case "runcmd":      //运行预定的cmd指令
        LB.ws_send.runCMD(content)
      break;

      case "server_info":
        LB.ws_send.getServerInfo()
      break;

      case "server_status":
        let cpuUsage = LB.HELPER.SYSINFO.cpuUsage
        let memUsage = LB.HELPER.SYSINFO.memUsage
        let diskUsage = LB.HELPER.SYSINFO.diskUsage
        let sysUptime = LB.HELPER.SYSINFO.sysUptime
        LB.bot.toMgmtGroup(LB.HELPER.MSG.getGroupEvent("server_status_get",cpuUsage,memUsage,diskUsage,sysUptime))
      break;

      case "motdpe":   //获取服务器版本信息
        let url = msg.replace(LB.HELPER.REGEX.regexBody,'$1')
        LB.EXTENSION.motdpe(url)
      break;

      case "runcmd_raw":  //运行自定义cmd指令
        let cmd = msg.replace(LB.HELPER.REGEX.regexBody,'$1')  //使用正则进行匹配
        LB.ws_send.runCMD(cmd)
      break;

      case "whitelist_bind":{  //自助绑定白名单
        let xboxid = msg.replace(LB.HELPER.REGEX.regexBody,'$1')
        if(LB.HELPER.APD.getXboxID(qq) != undefined){   //通过该qq查询xboxid，判断是否包含申请绑定的xboxid
          e.reply("[CQ:at,qq="+qq+"] "+LB.HELPER.MSG.getWhitelistEvent("member_already_binded",qq,xboxid))
          return
        }
        if(LB.HELPER.APD.XboxID_existed(xboxid) && LB.HELPER.APD.getQQ(xboxid) != qq){     //xboxid已存在，但xboxid对应的qq与申请绑定者的qq不一致
          e.reply("[CQ:at,qq="+qq+"] "+LB.HELPER.MSG.getWhitelistEvent("member_already_binded_by_others",qq,xboxid))
          return
        }
        LB.HELPER.APD.bindXboxID(qq,xboxid)
        LB.bot.setGroupCard(qq,xboxid)
        e.reply("[CQ:at,qq="+qq+"] "+LB.HELPER.MSG.getWhitelistEvent("member_bind_succeeded",qq,xboxid))
      }
      break;
      
      case "whitelist_unbind":{    //自助解绑白名单
        if(LB.HELPER.APD.getXboxID(qq) == undefined){   //查询不到该玩家qq对应的xboxid
          e.reply("[CQ:at,qq="+qq+"] "+LB.HELPER.MSG.getWhitelistEvent("member_not_bind",qq,LB.HELPER.APD.getXboxID(qq)))
          return
        }
        let xboxid = LB.HELPER.APD.getXboxID(qq)
        if(LB.HELPER.APD.getBindStatus(qq) == true){
          LB.ws_send.runCMD(`whitelist remove "${LB.HELPER.APD.getXboxID(qq)}"`)
        }
        LB.HELPER.APD.unbindXboxID(qq)
        e.reply("[CQ:at,qq="+qq+"] "+LB.HELPER.MSG.getWhitelistEvent("member_unbind_succeeded",qq,xboxid))
      }
      break;

      case "bind_check":      //查某人的绑定状态
        for(n in getAtQQ(e)){
          let xboxid = LB.HELPER.APD.getXboxID(getAtQQ(e)[n])
          if(xboxid == undefined){
            e.reply(LB.HELPER.MSG.getWhitelistEvent("target_member_not_bind",getAtQQ(e)[n],xboxid))
            return
          }
          e.reply("查询结果：\nQQ："+getAtQQ(e)[n]+"\nXboxID："+LB.HELPER.APD.getXboxID(getAtQQ(e)[n])+"\n权限："+playersInfo(getAtQQ(e)[n]).permission+"\n白名单状态："+playersInfo(getAtQQ(e)[n]).status)
        }
      break;

      case "bind_check_self": //查自己的绑定状态
        let xboxid = LB.HELPER.APD.getXboxID(qq)
        if(xboxid == undefined){
          e.reply("[CQ:at,qq="+qq+"] "+LB.HELPER.MSG.getWhitelistEvent("member_not_bind",qq,xboxid))
          return
        }
        e.reply("[CQ:at,qq="+qq+"] 您的信息：\nXboxID："+LB.HELPER.APD.getXboxID(qq)+"\n权限："+playersInfo(qq).permission+"\n白名单状态："+playersInfo(qq).status)
      break;

      case "whitelist_add":   //加某人的白名单
        for(n in getAtQQ(e)){
          let xboxid = LB.HELPER.APD.getXboxID(getAtQQ(e)[n])
          if(LB.HELPER.APD.getBindStatus(getAtQQ(e)[n]) == false){ //已绑定但未添加白名单
            LB.ws_send.runCMD(`whitelist add "${LB.HELPER.APD.getXboxID(getAtQQ(e)[n])}"`)
            LB.HELPER.APD.addWhitelist(getAtQQ(e)[n])
            e.reply(LB.HELPER.MSG.getWhitelistEvent("member_added_to_whitelist",qq,xboxid))
          }
          else if(LB.HELPER.APD.getBindStatus(getAtQQ(e)[n])){  //已添加过白名单
            e.reply(LB.HELPER.MSG.getWhitelistEvent("target_member_already_in_whitelist",getAtQQ(e)[n],xboxid))
          }
          else if(LB.HELPER.APD.getXboxID(getAtQQ(e)[n]) == undefined){ //未绑定
            e.reply(LB.HELPER.MSG.getWhitelistEvent("target_member_not_bind",getAtQQ(e)[n],xboxid))
          }
        }
      break;

      case "whitelist_add_self":{
        if(LB.HELPER.APD.getBindStatus(qq) == false){ //已绑定但未添加白名单
          LB.ws_send.runCMD(`whitelist add "${LB.HELPER.APD.getXboxID(qq)}"`)
          LB.HELPER.APD.addWhitelist(qq)
          e.reply(LB.HELPER.MSG.getWhitelistEvent("member_added_to_whitelist",qq,LB.HELPER.APD.getXboxID(qq)))
        }
        else if(LB.HELPER.APD.getBindStatus(qq)){  //已添加过白名单
          e.reply(LB.HELPER.MSG.getWhitelistEvent("member_already_in_whitelist",qq,LB.HELPER.APD.getXboxID(qq)))
        }
        else if(LB.HELPER.APD.getXboxID(qq) == undefined){ //未绑定
          e.reply(LB.HELPER.MSG.getWhitelistEvent("member_not_bind",qq))
        }
      }
      break;

      case "whitelist_del":   //删某人的白名单
        for(n in getAtQQ(e)){
          let xboxid = LB.HELPER.APD.getXboxID(getAtQQ(e)[n])
          if(xboxid != undefined){
            if(LB.HELPER.APD.getBindStatus(getAtQQ(e)[n]) == true){
              LB.ws_send.runCMD(`whitelist remove "${LB.HELPER.APD.getXboxID(getAtQQ(e)[n])}"`)
            }
            LB.HELPER.APD.unbindXboxID(getAtQQ(e))
            e.reply(LB.HELPER.MSG.getWhitelistEvent("target_member_unbind_succeeded",getAtQQ(e)[n],xboxid))
          }
          else{
            e.reply(LB.HELPER.MSG.getWhitelistEvent("target_member_not_bind",getAtQQ(e)[n]))
          }
        }
      break;

      case "http_get":
        //console.log(LB.HELPER.HTTPGET(content))
        LB.HELPER.HTTPGET(content,(res)=>{
          if(res == false){
            e.reply("[CQ:at,qq="+qq+"] "+LB.HELPER.MSG.getHttpEvent("failed"))
            return
          }
          e.reply("[CQ:at,qq="+qq+"] "+LB.HELPER.MSG.getHttpEvent("succeed"))
      })
      break;

      case "blackbe_check":
        let check_way = msg.replace(LB.HELPER.REGEX.regexBody,'$1')
        let target = msg.replace(LB.HELPER.REGEX.regexBody,'$2')
        switch(check_way){
          case "qq":    //使用qq进行查询
            if(target != target.match(/[1-9][0-9]{4,10}/g)){                                  //如果查询目标不是正确格式的qq号
              if(getAtQQ(e)[0] == undefined){                                                 //则判断查询目标是不是at他人，如果也不是
                LB.bot.toMgmtGroup(LB.HELPER.MSG.getGroupEvent("blackbe_check_target_error")) //则直接提示查询格式不正确
                return                                                                        //并终止执行
              }
              for(m in getAtQQ(e)){  //获取at目标的qq，通过at目标的qq进行查询
                LB.EXTENSION.blackbe(check_way,getAtQQ(e),0)
              }
            }
            else{
              //LB.EXTENSION.blackbe(check_way,target,0)        //则直接用qq号查询
              LB.EXTENSION.blackbe(check_way,target,0)
            }
          break;

          case "xboxid":  //使用xboxid进行查询
            if(target != target.match(/([A-Za-z0-9 ]{4,20})/g)){
              LB.bot.toMgmtGroup(LB.HELPER.MSG.getGroupEvent("blackbe_check_target_error"))
              return
            }
            LB.EXTENSION.blackbe(check_way,target,0)
          break;
        }
      break;
    }
  }
}

function ChatGroup(e){
  if(e.group_id != arrChatGroup){   //发信人所在群和设置的不一致，则直接返回，不执行
    return;
  }
  if(LB.CONFIG.global.features.chat_forward != true){
    return
  }
  LB.HELPER.MSG.getGroupEvent("chat_to_server",e.group_name,e.sender.card,e.raw_message)
}

function getAtQQ(e){    //获取at目标的qq，获取到则返回一个数组，获取不到返回undefined
  var atid = [];
  for(b in e.message){
    if(e.message[b].type == "at"){
      atid.push(e.message[b].data.qq);
    }
  }
  return atid;
}

function playersInfo(qq){   //玩家信息检索，获取到返回对象，获取不到返回false
  let xboxid = LB.HELPER.APD.getXboxID(qq)
  if(xboxid == false){
    return false
  }
  switch(LB.HELPER.APD.getPermission(qq)){
    case 0: var permission = "普通成员";break;
    case 1: var permission = "管理员";break;
  }

  switch(LB.HELPER.APD.getBindStatus(qq)){
    case true : var status = "已绑定，已添加";break;
    case false : var status = "已绑定，未添加";break;
  }
  return {permission,status}
}


LB.MAIN.GROUP.push(MgmtGroup)
LB.MAIN.GROUP.push(ChatGroup)

/*
{
  self_id: 3166155101,
  time: 1636605169,
  post_type: 'message',
  message_type: 'group',
  sub_type: 'normal',
  message_id: 'Odly+DErFosAAUXefDYmJGGMnPEB',
  group_id: 970552056,
  group_name: 'Dreamworks Ready',
  user_id: 824907403,
  anonymous: null,
  message: [ { type: 'text', data: [Object] } ],
  raw_message: '查服',
  atme: false,
  block: false,
  seqid: 83422,
  font: '宋体',
  sender: {
    user_id: 824907403,
    nickname: 'Asurin',
    card: 'Asulin219',
    sex: 'female',
    age: 22,
    area: 'unknown',
    level: 1,
    role: 'owner',
    title: ''
  },
  reply: [Function (anonymous)]
}
*/